﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone;
using RutoDeBared.Control;
using System.Windows.Resources;

namespace RutoDeBared
{
    public partial class InformatieScherm : PhoneApplicationPage
    {
        Informatie i = new Informatie();

        public InformatieScherm()
        {
            InitializeComponent();
            if (Page3.taal.Equals("nederlands"))
            {
                ApplicationTitle.Text = "Informatie";
            }
            else
            {
                ApplicationTitle.Text = "Information";
            }
            textBlock1.Text = "naam: " + i.getHuidigPunt().Naam + Environment.NewLine + "categorie: " + i.getHuidigPunt().Categorie + Environment.NewLine + i.getHuidigPunt().Info;
            //plaatje laden en aan img geven
            openenIsoStorage(i.getHuidigPunt().imagePath);
            
        } 

        //jpg laden uit isolated storage nog niet aangepast
        private void openenIsoStorage(string fileName)
        {
            fileName = @"Afbeeldingen/" + fileName;
            WriteableBitmap bitmap = new WriteableBitmap(200, 200);

            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, isoStore))
                    {
                        bitmap = PictureDecoder.DecodeJpeg(isoStream);
                    }

                    //afbeelding weergeven
                    image1.Source = bitmap;
                }
                catch (Exception)
                {
                    MessageBox.Show("Geen afbeelding voor deze bezienswaardigheid gevonden.");
                }
            }
        }

        //plaatje opslaan in isolated storage om vervolgens de bovestaande methode te testen
        private void OpslaanIsoStorage(BitmapImage bmi, string fileName)
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(fileName))
                {
                    myIsolatedStorage.DeleteFile(fileName);
                }

                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(fileName);

                WriteableBitmap wb = new WriteableBitmap(bmi);
                wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();
            }
            openenIsoStorage(i.getHuidigPunt().imagePath);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
    }
}